<!-- 包装 -->
<template>
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      label-width="60px"
      size="large"
      @submit.prevent
    >
      <el-form-item label="站别">
        <el-select
          v-model="formInline.station"
          placeholder="请选择站别"
          clearable
          @change="changeStation"
        >
          <el-option
            v-for="item in stationList"
            :key="item.Value"
            :label="item.Label"
            :value="item.Value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="SN">
        <el-input
          class="my-sn"
          ref="snRef"
          v-model="formInline.sn"
          placeholder="请扫描或者输入SN"
          :suffix-icon="CircleCheck"
          @focus="focusSN"
          @keyup.enter.prevent="handleSN"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="onReset">重&nbsp;&nbsp;置</el-button>
      </el-form-item>
      <br />
      <el-form-item label="结果">
        <el-select v-model="formInline.result" placeholder="请选择结果" clearable>
          <el-option label="Pass" value="Pass" />
          <el-option label="Fail" value="Fail" />
        </el-select>
      </el-form-item>
      <el-form-item label="不良">
        <el-input v-model="formInline.errDescr" placeholder="请输入不良" />
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="onSubmit">提&nbsp;&nbsp;交</el-button>
      </el-form-item>
    </el-form>
    <div style="height: 550px">
      <el-table
        :data="list"
        :header-cell-style="{ background: '#eaf6fb' }"
        stripe
        border
        size="large"
        style="width: 100%"
      >
        <el-table-column
          v-for="(key, index) in column"
          :key="index"
          align="center"
          :prop="key"
          :label="key"
          show-overflow-tooltip
        />
      </el-table>
    </div>
  
    <Tips :tips="tips" />
  </template>
  
  <script lang="ts" setup>
  import { useMessage } from '@/hooks/useMessage'
  import { objectToQueryParams } from '@/utils/objectToQuery'
  import { onMounted, reactive, ref } from 'vue'
  import * as BasicApi from '@/api'
  import { CircleCheck } from '@element-plus/icons-vue'
  import Tips from '../common/tips/Index.vue'
  
  //定义接收的props
  const props = defineProps({
    // 定义一个对象类型的 prop
    data: {
      type: Object,
      default: () => ({ groupName: '', jobType: '' })
    }
  })
  
  const basicParams = reactive({
    groupName: props.data.groupName,
    jobType: props.data.jobType
  })
  
  const formInline = reactive({
    sn: '',
    station: '',
    result: 'Pass',
    errDescr: ''
  })
  
  const tips = reactive({
    code: 200,
    message: ''
  })
  
  const snRef = ref<HTMLInputElement | null>(null)
  const focusSN = () => {
    snRef.value?.focus()
  }
  const selectAllSN = () => {
    snRef.value?.select()
  }
  
  //将光标聚焦到SN
  const changeStation = () => {
    formInline.sn = ''
    formInline.station && focusSN()
  }
  
  //刷入SN后调用接口 获取列表
  const handleSN = async () => {
    const post = {
      ...basicParams,
      operType: 'SNCheck',
      station: formInline.station,
      sn: formInline.sn
    }
    const inputStr = objectToQueryParams(post)
    const res = (await BasicApi.Query({ inputStr })) as any
    tips.code = res.code
    tips.message = res.msg
    if (res.code !== 200) {
      message.error(res.msg)
      return
    }
    //更新list
    list.value = res.data.listInfo || []
    column.value = list.value.length && Object.keys(list.value[0]) || []
  }
  
  const list = ref<any[]>([])
  const column = ref<any[]>([])
  
  const onSubmit = async () => {
    const post = {
      ...basicParams,
      operType: 'Routing',
      ...formInline
    }
    const inputStr = objectToQueryParams(post)
    const jsonData = list.value
    const res = (await BasicApi.Query({ inputStr, jsonData })) as any
    tips.code = res.code
    tips.message = res.msg
    if (res.code !== 200) {
      message.error(res.msg)
      return
    }
    message.success(res.msg)
    onReset()
  }
  
  const onReset = () => {
    formInline.sn = ''
    focusSN()
    formInline.result = 'Pass'
    formInline.errDescr = ''
    list.value = []
    tips.code = 200
    tips.message = ''
  }
  
  const message = useMessage()
  
  const stationList = ref<any[]>([])
  const getStationList = async () => {
    const post = {
      ...basicParams,
      operType: 'GetValue',
      key: 'Station'
    }
    const inputStr = objectToQueryParams(post)
    const res = (await BasicApi.Query({ inputStr })) as any
    if (res.code !== 200) {
      message.error(res.msg)
      return
    }
    stationList.value = res.data.listInfo || []
  }
  onMounted(() => {
    getStationList()
  })
  </script>
  
  <style scoped lang="less">
  :deep(.el-form-item__label) {
    font-size: 16px;
  }
  .demo-form-inline .el-input {
    --el-input-width: 350px;
  }
  
  .demo-form-inline .el-select {
    width: 280px;
  }
  
  :deep(.el-input .el-input__inner) {
    font-size: 16px;
  }
  
  :deep(.my-sn .el-input__wrapper) {
    background: #f0f9eb;
    border: 1px solid #67c23a;
    box-shadow: none;
  }
  :deep(.el-input .el-input__icon) {
    font-size: 18px;
    color: #67c23a;
  }
  :deep(.el-table__empty-block) {
    height: 500px !important;
  }
  </style>
  